/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package p4_nb;

import java.awt.Point;
import java.sql.*;
import java.util.Random;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class Main
{

    static Connection con;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        try {
            try {
                Class.forName("org.postgresql.Driver");
            } catch (ClassNotFoundException e) {
            }
            String url = "jdbc:postgresql://localhost:5432/postgis";
            try {
                con = DriverManager.getConnection(url, "postgres", "hda");
                con.setAutoCommit(false);
                System.out.println("Datenbank verbunden!");
            } catch (SQLException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }

            String dtable = "DROP TABLE IF EXISTS Waylines";
            Statement c;
            c = con.createStatement();
            c.execute(dtable);
            con.commit();

            String ctable = "CREATE TABLE Waylines(id INTEGER primary key, geom geometry)";
            c.execute(ctable);
            con.commit();

            insertRandomData();

            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    static public void insertRandomData() throws SQLException
    {
        String line = "'LINESTRING(";
        PreparedStatement ps = con.prepareStatement("INSERT INTO Waylines VALUES (?,ST_LineFromText(?));");

        for(int k=0; k < 10000; k++)
        {
            line = "LINESTRING(";
            Vector<Point> v1 = createRandomLine();
            for(int j=0; j < v1.size(); j++)
            {
                line += v1.elementAt(j).x+" "+v1.elementAt(j).y;
                if( j != v1.size()-1) line += ",";
            }
            line += ")";
            try
            {
                ps.setInt(1, k+1);
                ps.setString(2, line);
                ps.execute();
                con.commit();
            } catch (SQLException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ps.toString());
            }

        }
    }

    static public Vector<Point> createRandomLine()
    {
        Vector<Point> v = new Vector<Point>();
        Random r = new Random();
        //int ind = r.nextInt(15)+5;

        //for(int i=0; i < ind; i++)
        //{
        int a,b;
        a = r.nextInt(10000);
        b = r.nextInt(10000);
        v.add(new Point(a,b));
        v.add(new Point(a+r.nextInt(1000),b+r.nextInt(1000)));
        //}

        v.add(v.elementAt(0));

        return v;
    }
}
